import * as vscode from 'vscode';
import ProjectProvider from './provider/projectProvider';

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

	let watchEvent = vscode.commands.registerCommand('bundleCloud.watchEntry', (item) => {
		const cloneItem = {...item};
		cloneItem.collapsibleState = 1;
		projectWatchProvider.watch(cloneItem);
	});

	let removeEvent = vscode.commands.registerCommand('bundleCloud.RemoveEntry', (item) => {
		projectWatchProvider.remove(item);
	});

	let copyEvent = vscode.commands.registerCommand('bundleCloud.CopyLink', (item) => {
		// console.log(item);
		vscode.env.clipboard.writeText(item.address).then(onFulfulled => {
			vscode.window.setStatusBarMessage('复制地址成功！');
			setTimeout(() => {
				vscode.window.setStatusBarMessage('');
			}, 3000);
		});
	});

	let refreshProjectListEvent = vscode.commands.registerCommand('bundleCloud.RefreshProjectList', (item) => {
		// console.log(item);
		projectListProvider.init();
	});

	let refreshProjectWatchEvent = vscode.commands.registerCommand('bundleCloud.RefreshProjectWatch', (item) => {
		// console.log(item);
		projectWatchProvider.refresh();
	});

	const projectListProvider = new ProjectProvider(context);
	const projectWatchProvider = new ProjectProvider(context, 'watch');

	context.subscriptions.push(watchEvent);
	context.subscriptions.push(removeEvent);
	context.subscriptions.push(copyEvent);
	context.subscriptions.push(refreshProjectListEvent);
	context.subscriptions.push(refreshProjectWatchEvent);

	vscode.window.createTreeView('project-list', {
		treeDataProvider: projectListProvider
	});

	vscode.window.createTreeView('project-watch', {
		treeDataProvider: projectWatchProvider
	});
}

// this method is called when your extension is deactivated
export function deactivate() { }
